let { personCheck, personList, list } = require("./mock");
const bodyParser = require("body-parser");
const Mock = require("mockjs");
module.exports = (app) => {
  app.use(bodyParser.json());
  app.get("/api/person/check", (req, res) => {
    const { value = "" } = req.query;
    const result = personCheck.filter((v) => v.name.includes(value));
    res.send({
      code: 200,
      data: result,
    });
  });

  app.get("/api/person/check/delete", (req, res) => {
    const { id } = req.query;
    personCheck = personCheck.filter((v) => {
      return v.id !== id;
    });
    res.send({
      code: 200,
      message: "删除成功",
    });
  });

  app.get("/api/person/check/:id", (req, res) => {
    const { id } = req.params;
    personCheck = personCheck.filter((v) => {
      if (v.id === id) {
        personList.unshift({ ...v });
      }
      return v.id !== id;
    });
    res.send({
      code: 200,
      message: "审核成功",
    });
  });

  app.get("/api/person/list", (req, res) => {
    res.send({
      code: 200,
      data: personList,
    });
  });

  app.post("/api/person/add", (req, res) => {
    req.body.id = Math.random().toString().slice(2);
    personCheck.unshift(req.body);
    res.send({
      code: 200,
    });
  });

  app.post("/api/detail", (req, res) => {
    const { id } = req.body;
    const result = personList.find((v) => v.id === id);
    res.send({
      code: 200,
      data: {
        data: result,
        prices: Mock.mock({
          "list|12": ["@integer(10000, 20000)"],
        }).list,
      },
    });
  });

  app.get("/api/car", (req, res) => {
    const { pageCode = 1, pageSize = 16, price, type, desc, value } = req.query;

    const newList = [...list]
      .filter(v => {
        return v.name.includes(value)
      })
      .filter((v) => {
        switch (price) {
          case "a":
            return true;
          case "b":
            return v.price >= 0 && v.price < 5;
          case "c":
            return v.price >= 5 && v.price < 10;
          case "d":
            return v.price >= 10 && v.price < 20;
          case "e":
            return v.price >= 20 && v.price < 50;
          case "f":
            return v.price >= 50;
        }
      })
      .filter((v) => {
        if (type === "不限") return true;
        return v.type === type;
      })
      .sort((a, b) => {
        switch (desc) {
          case "a":
            return a.price - b.price;
          case "b":
            return b.price - a.price;
          case "c":
            return a.score - b.score;
          case "d":
            return b.score - a.score;
          default:
            return;
        }
      });

    res.send({
      code: 200,
      data: newList.slice((pageCode - 1) * pageSize, pageCode * pageSize),
    });
  });
};
